<template>
  <div>
	  
	  
	  
    <!-- 搜索区域 -->
    <el-form :inline="true" :model="userInline" class="demo-form-inline">
      <el-form-item label="用户名">
        <el-input
          clearable
          @clear="queryUser"
          v-model="userInline.username"
          placeholder="请输入用户名"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="queryUser">搜索</el-button>
        <el-button type="primary" @click="toInsert">新增</el-button>
      </el-form-item>
    </el-form>
    <!-- 表格内容 -->
	
	
	
	
    <el-table :data="userList" border style="width: 100%">
      <el-table-column prop="id" label="编号"> </el-table-column>
      <el-table-column prop="username" label="用户名"> </el-table-column>
      <el-table-column prop="nickname" label="昵称"></el-table-column>
      <el-table-column prop="email" label="邮箱"> </el-table-column>
      <el-table-column prop="phone" label="手机"> </el-table-column>
      <el-table-column prop="point" label="积分"> </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-link type="primary" @click="toUpdate(scope.row)">编辑</el-link>
          <el-link type="primary" @click="toDelete(scope.row.id)">删除</el-link>
        </template>
        <!-- <el-link type="primary" @click="toUpdate">编辑</el-link>
        <el-link type="primary">删除</el-link> -->
      </el-table-column>
    </el-table>
	
	
    <!-- 分页 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="userInline.pageNum"
      :page-sizes="[5, 10, 15, 20]"
      :page-size="100"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    >
    </el-pagination>
	
	
	
    <!-- 新增修改弹出框 -->
    <el-dialog :title="title" :visible.sync="dialogFormVisible" @close="cancal">
      <el-form :model="user">
        <el-form-item label="用户名" label-width="80px">
          <el-input
            v-model="user.username"
            placeholder="请输入用户名"
          ></el-input>
        </el-form-item>
        <el-form-item label="密码" label-width="80px">
          <el-input v-model="user.password" placeholder="请输入密码"></el-input>
        </el-form-item>
        <el-form-item label="昵称" label-width="80px">
          <el-input v-model="user.nickname" placeholder="请输入昵称"></el-input>
        </el-form-item>
        <el-form-item label="邮箱" label-width="80px">
          <el-input v-model="user.email" placeholder="请输入邮箱"></el-input>
        </el-form-item>
        <el-form-item label="手机" label-width="80px">
          <el-input v-model="user.phone" placeholder="请输入手机"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancal">取 消</el-button>
        <el-button type="primary" @click="insertAndUpdateSuccess"
          >确 定</el-button
        >
      </div>
    </el-dialog>
	
	
  </div>
</template>




<script>
export default {
  data() {
    return {
      userInline: {
        username: "",
        pageNum: 1,
        pageSize: 5,
      },
      userList: [],
      total: 6,
      title: "",
      user: {
        id: "",
        username: "",
        password: "",
        nickname: "",
        email: "",
        phone: "",
        point: "",
      },
      dialogFormVisible: false,
    };
  },
  methods: {
    //取消清空
    cancal() {
      this.dialogFormVisible = false;
      this.user = {
        id: "",
        username: "",
        password: "",
        nickname: "",
        email: "",
        phone: "",
        point: "",
      };
      this.queryUser();
    },
    //删除
    toDelete(id) {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.$http.delete("user/" + id).then((res) => {
            if (res.data.status == 200) {
              this.$message.success("删除成功");
              this.queryUser();
            } else {
              this.$message.error("删除失败");
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    //前往编辑页面
    toUpdate(row) {
      this.title = "编辑";
      //赋值
      this.user = row;
      this.dialogFormVisible = true;
    },
    //添加和修改
    insertAndUpdateSuccess() {
      if (this.user.id) {
        this.$http.put("user", this.user).then((res) => {
          if (res.data.status == 200) {
            this.$message.success("修改成功");
            this.user = {
              id: "",
              username: "",
              password: "",
              nickname: "",
              email: "",
              phone: "",
              point: "",
            };
            //关闭对话框
            this.dialogFormVisible = false;
            //从新查询
            this.queryUser();
          } else {
            this.$message.error("修改失败");
          }
        });
      } else {
        this.$http.post("user", this.user).then((res) => {
          if (res.data.status == 200) {
            this.$message.success("添加成功");
            this.user = {
              id: "",
              username: "",
              password: "",
              nickname: "",
              email: "",
              phone: "",
              point: "",
            };
            //关闭对话框
            this.dialogFormVisible = false;
            //从新查询
            this.queryUser();
          } else {
            this.$message.error("用户名已存在");
          }
        });
      }
      this.fileList = [];
    },
    //前往添加页面
    toInsert() {
      this.title = "新增";
      this.dialogFormVisible = true;
    },
    handleSizeChange(val) {
      //   console.log(`每页 ${val} 条`);
      this.userInline.pageSize = val;
      this.userInline.pageNum = 1;
      this.queryUser();
    },
    handleCurrentChange(val) {
      //   console.log(`当前页: ${val}`);
      this.userInline.pageNum = val;
      this.queryUser();
    },
    queryUser() {
      this.$http
        .get("user", {
          params: this.userInline,
        })
        .then((res) => {
          if (res.data.status == 200) {
            this.userList = res.data.data.list;
            this.total = res.data.data.total;
          } else {
            this.$message.error("查询失败");
          }
        });
    },
  },
  mounted() {
    this.queryUser();
  },
};
</script>
<style>
.el-pagination {
  margin-top: 15px;
}
.el-link {
  padding: 0 8px;
}
.ql-container {
  min-height: 200px;
}
</style>